<?php

class FollowController extends MController
{
	public function actionAddfollow()
	{
		$request = array();
		$params = array('userid', 'followid', 'expiretime', 'iswomen');
		foreach ($params as $item) {
			$request[$item] = $this->getRequest($item);
		}
		
		if ($this->model('guanzhu')->isExist($request['userid'], $request['followid'])) {
			//$this->return_json(1, '您已经订阅过这个女神了！');
		}
		
		
		//如果是关注女神则扣费
		//if ($this->model('user')->isGirl($request['followid'])) {
			$iswomen = (int) $request['iswomen'];
			if ($iswomen == 1) {
				$expiretime = intval($request['expiretime']);
				$follow_price = $this->model('other')->findByPk(1);
				//$howmuchs = array(1 => '380',  3 => '1080', 6 => '1980', 12 => '3680', 99 => '50000');
				$howmuchs = array(
					1 => $follow_price['follow_girl1'],  
					3 => $follow_price['follow_girl2'], 
					6 => $follow_price['follow_girl3'], 
					12 => $follow_price['follow_girl4'], 
					99 => $follow_price['follow_girl5']
				);
				$howmuch = $howmuchs[$expiretime];
			} else {
				$howmuch = 38;
				$expiretime = 1;
			}
			
			$time = time();
			$data = array(
				'buserid' => $request['followid'],
				'userid' => $request['userid'],
				'expiretime' => strtotime('+'. $expiretime .' month', $time),
				'createtime' => $time
			);
			
			$usermoney = (int) $this->model('UserMoney')->getMoney($request['userid']);
			if ($usermoney < $howmuch) {
				$this->return_json(1, '余额不足');
			}
			$this->model('UserMoney')->updateMoney($request['userid'], $howmuch);
			$this->model('consume')->insert(array(
				'userid' => $request['userid'],
				'itemid' => 0,
				'howmuch' => $howmuch,
				'createtime' => $time,
				'sorpuserid' => $request['followid'],
				'consumetype' => 2
			));
			
			//第一次关注官方账号
			if ($request['followid'] == USERID && !$this->model('TaskFollow')->isExist($request['userid'], USERID)) {
				$howmuch = 38;
				$finance = array(
					'uid' => $request['userid'],
					'paytype' => '赠送第一次订阅官方账号',
					'howmuch' => $howmuch,
					'ordernum' => $time,
					'createtime' => $time
				);
				if ($this->model('Financedetail')->insert($finance) !== false) {
					$this->model('UserMoney')->addMoney($request['userid'], $howmuch);
					$this->model('TaskFollow')->insert(array(
						'userid' => $request['userid'],
						'follow_userid' => USERID,
						'addtime' => $time
					));
				}
			}
			
			if ($this->model('guanzhu')->insert($data) !== false) {
				$this->return_json(0, '订阅成功');
			}
		//} else {
		//	$this->return_json(1, '此女神不存在');
		//}
		
		
		$this->return_json(1, '订阅失败');
	}
	
	/**
	 * 我的粉丝
	 */
	public function actionMyfans()
	{
		$request = array();
		$params = array('pageno', 'pagesize', 'userid');
		foreach ($params as $item) {
			$request[$item] = $this->getRequest($item);
		}
		!$request['pageno'] && $request['pageno'] = 1;
		!$request['pagesize'] && $request['pagesize'] = 10;
		$fans = $this->model('guanzhu')->getListFans($request['userid'], $request['pageno'], $request['pagesize']);
		if (!empty($fans)) {
			foreach ($fans as $i => $item) {
				$user = $this->model('user')->getUserPhoto($item['userid'], 'u.nickname as truename,u.sign,p.midpath logopath');
				$fans[$i]['logopath'] = !empty($user['logopath']) ? PHOTO_URL . '/' . $user['logopath'] : '';
				$fans[$i]['truename'] = $user['truename'];
				$fans[$i]['sign'] = $user['sign'];
				$fans[$i]['followtime'] = date('Y-m-d H:i:s', $item['followtime']);
				//$fans[$i]['howmuch'] = $this->model('Consume')->getFansHowmuch($item['userid'], $request['userid'], 2);
			}
		}
		$result['code'] = 0;
		$result['msg'] = '成功';
		$result['followes'] = $fans;
		echo json_encode($result);die;
	}
	
	/**
	 * 我的关注
	 */
	public function actionMyfocus()
	{
		$request = array();
		$params = array('pageno', 'pagesize', 'userid', 'isall');
		foreach ($params as $item) {
			$request[$item] = $this->getRequest($item);
		}
		!$request['pageno'] && $request['pageno'] = 1;
		!$request['pagesize'] && $request['pagesize'] = 30;
		if (isset($request['isall']) && $request['isall'] == 1) {
			$fans = $this->model('guanzhu')->getListByUserIdAll($request['userid'], $request['pageno'], $request['pagesize'], $request['isall']);
			if (!empty($fans)) {
				foreach ($fans as $i => $item) {
					if ($item['buserid'] == $request['userid']) {
						$fans[$i]['myfocus'] = '0';
						$userid = $item['userid'];
					} else {
						$fans[$i]['myfocus'] = '1';
						$userid = $item['buserid'];
					}
					$fans[$i]['buserid'] = $userid; 
					$user = $this->model('user')->getUserPhoto($userid, 'u.nickname as truename,u.sign,p.midpath logopath');
					if (!empty($user)) {
						$fans[$i]['truename'] = $user['truename'];
						$fans[$i]['sign'] = $user['sign'];
						$fans[$i]['logopath'] = !empty($user['logopath']) ? PHOTO_URL . '/' . $user['logopath'] : '';
					} else {
						$fans[$i]['truename'] = '';
						$fans[$i]['sign'] = '';
						$fans[$i]['logopath'] = '';
					}
				}
			}
		} else {
			$fans = $this->model('guanzhu')->getListByUserId($request['userid'], $request['pageno'], $request['pagesize']);
			if (!empty($fans)) {
				foreach ($fans as $i => $item) {
					$fans[$i]['myfocus'] = '1';
					$fans[$i]['logopath'] = !empty($fans[$i]['logopath']) ? PHOTO_URL . '/' . $item['logopath'] : '';
				}
			}
		}
		$result['code'] = 0;
		$result['msg'] = '成功';
		$result['beauties'] = $fans;
		echo json_encode($result);die;
	}
	
	/*
	public function actionMyfocusdetails()
	{
		$request = array();
		$params = array('pageno', 'pagesize', 'userid');
		foreach ($params as $item) {
			$request[$item] = $this->getRequest($item);
		}
		!$request['pageno'] && $request['pageno'] = 1;
		!$request['pagesize'] && $request['pagesize'] = 10;
		$fans = $this->model('guanzhu')->getListByUserId($request['userid'], $request['pageno'], $request['pagesize']);
		$buserid = array();
		if (!empty($fans)) {
			foreach ($fans as $i => $item) {
				$fans[$i]['logopath'] = !empty($fans[$i]['logopath']) ? PHOTO_URL . '/' . $item['logopath'] : '';
				$feed = $this->model('item')->getLastFeed($item['buserid'], 'i.remark,i.itemid');
				//$feed = $this->model('item')->getFeedAll($item['buserid'], 'i.remark,i.itemid');
				if ($feed) {
					$fans[$i]['remark'] = $feed['remark'];
					$photoid = $this->model('ItemPhoto')->getListByItemID($feed['itemid'], 'GROUP_CONCAT(photoid) photoid');
					if (!empty($photoid)) {
						$photoid = $photoid[0]['photoid'];
						$photo = $this->model('photo')->getListIn($photoid);
						if (!empty($photo)) {
							foreach ($photo as $p) {
								$fans[$i]['photos'][] = array(
									'picurl' => PHOTO_URL . '/' . $p['path'],
									'picurll' => PHOTO_URL . '/' . $p['largepath'],
									'picurlm' => PHOTO_URL . '/' . $p['midpath'],
									'picurls' => PHOTO_URL . '/' . $p['smallpath']
								);
							}
						}
					}
					$fans[$i]['replynum'] = $this->model('reply')->getCountByItemID($feed['itemid'], 2);
				} else {
					$fans[$i]['remark'] = '';
					$fans[$i]['photos'] = array();
					$fans[$i]['replynum'] = '0';
				}
			}
			$result['beauties'] = $fans;
		}
		$result['code'] = 0;
		$result['msg'] = '成功';
		echo json_encode($result);die;
	}
	*/
	
	public function actionMyfocusdetails()
	{
		$request = array();
		$params = array('pageno', 'pagesize', 'userid');
		foreach ($params as $item) {
			$request[$item] = $this->getRequest($item);
		}
		!$request['pageno'] && $request['pageno'] = 1;
		!$request['pagesize'] && $request['pagesize'] = 10;
		$userids = $this->model('guanzhu')->getBuserids($request['userid']);
		$result['code'] = 0;
		$result['msg'] = '成功';
		$result['beauties'] = array();
		if (!$userids) {
			echo json_encode($result);die;
		}
		$beauties = $this->model('item')->getFeedAll($userids, 'i.remark,i.createtime,i.itemtype,i.itemid,u.nickname truename ,u.sign,p.midpath logopath,u.userid buserid', $request['pageno'], $request['pagesize'], 2);
		if (!empty($beauties)) {
			foreach ($beauties as $i => $item) {
				$beauties[$i]['logopath'] = !empty($beauties[$i]['logopath']) ? PHOTO_URL . '/' . $item['logopath'] : '';
				$beauties[$i]['createtime'] = date('m-d H:i', $item['createtime']);
				$photoid = $this->model('ItemPhoto')->getListByItemID($item['itemid'], 'GROUP_CONCAT(photoid) photoid');
				if (!empty($photoid)) {
					$photoid = $photoid[0]['photoid'];
					$photo = $this->model('photo')->getListIn($photoid);
					if (!empty($photo)) {
						foreach ($photo as $p) {
							$beauties[$i]['photos'][] = array(
								'picurl' => PHOTO_URL . '/' . $p['path'],
								'picurll' => PHOTO_URL . '/' . $p['largepath'],
								'picurlm' => PHOTO_URL . '/' . $p['midpath'],
								'picurls' => PHOTO_URL . '/' . $p['smallpath']
							);
						}
					}
				}
				$beauties[$i]['replynum'] = $this->model('reply')->getCountByItemID($item['itemid'], 2);
			}
			$result['beauties'] = $beauties;
		}
		echo json_encode($result);die;
	}
	
	/**
	 * 新增
	 */

	public function actionLiketweet(){
		$this->return_json(0, '点赞成功');
	}

	public function actionMytweets()
	{
		$request = array();
		$params = array('pageno', 'pagesize', 'userid');
		foreach ($params as $item) {
			$request[$item] = $this->getRequest($item);
		}
		!$request['pageno'] && $request['pageno'] = 1;
		!$request['pagesize'] && $request['pagesize'] = 10;
		$userids = $this->model('guanzhu')->getBuserids($request['userid']);
		$result['code'] = 0;
		$result['msg'] = '成功';
		$result['beauties'] = array();
		if (!$userids) {
			echo json_encode($result);die;
		}
		$beauties = $this->model('item')->getFeedAll($userids, 'i.remark,i.createtime,i.itemtype,i.itemid,u.nickname truename ,u.sign,p.midpath logopath,u.userid buserid', $request['pageno'], $request['pagesize'], 2);
		if (!empty($beauties)) {
			foreach ($beauties as $i => $item) {
				$beauties[$i]['logopath'] = !empty($beauties[$i]['logopath']) ? PHOTO_URL . '/' . $item['logopath'] : '';
				$beauties[$i]['createtime'] = date('m-d H:i', $item['createtime']);
				$photoid = $this->model('ItemPhoto')->getListByItemID($item['itemid'], 'GROUP_CONCAT(photoid) photoid');
				if (!empty($photoid)) {
					$photoid = $photoid[0]['photoid'];
					$photo = $this->model('photo')->getListIn($photoid);
					if (!empty($photo)) {
						foreach ($photo as $p) {
							$beauties[$i]['photos'][] = array(
								'picurl' => PHOTO_URL . '/' . $p['path'],
								'picurll' => PHOTO_URL . '/' . $p['largepath'],
								'picurlm' => PHOTO_URL . '/' . $p['midpath'],
								'picurls' => PHOTO_URL . '/' . $p['smallpath']
							);
						}
					}
				}
				$beauties[$i]['replynum'] = $this->model('reply')->getCountByItemID($item['itemid'], 2);
			}
			$result['beauties'] = $beauties;
		}
		echo json_encode($result);die;
	}


	public function actionHottweets()
	{

		

		// 暂时适用我所关注
		$request = array();
		$params = array('pageno', 'pagesize', 'userid');
		foreach ($params as $item) {
			$request[$item] = $this->getRequest($item);
		}
		!$request['pageno'] && $request['pageno'] = 1;
		!$request['pagesize'] && $request['pagesize'] = 10;
		$userids = $this->model('guanzhu')->getBuserids($request['userid']);
		$result['code'] = 0;
		$result['msg'] = '成功';
		$result['tweets'] = array();
		if (!$userids) {
			echo json_encode($result);die;
		}
		$beauties = $this->model('item')->getFeedAll($userids, 'i.remark,i.createtime,i.itemtype,i.itemid,u.nickname truename ,u.sign,p.midpath logopath,u.userid buserid', $request['pageno'], $request['pagesize'], 2);
		if (!empty($beauties)) {
			foreach ($beauties as $i => $item) {
				$beauties[$i]['logopath'] = !empty($beauties[$i]['logopath']) ? PHOTO_URL . '/' . $item['logopath'] : '';
				// $beauties[$i]['createtime'] = date('m-d H:i', $item['createtime']);
				$beauties[$i]['createtime'] = time() - 1000;

				// isreply 字段表示是否有评论这条动态
				// $beauties[$i]['isreply'] = true;
				// islik 字段表示是否有点赞这条动态
				// $beauties[$i]['islike'] = true;

				$photoid = $this->model('ItemPhoto')->getListByItemID($item['itemid'], 'GROUP_CONCAT(photoid) photoid');
				if (!empty($photoid)) {
					$photoid = $photoid[0]['photoid'];
					$photo = $this->model('photo')->getListIn($photoid);
					if (!empty($photo)) {
						foreach ($photo as $p) {
							// 此处可有4张图片,但只有一种大小
							$beauties[$i]['photos'][] = PHOTO_URL . '/' . $p['midpath'];
						}
					}
				}
				$beauties[$i]['replynum'] = $this->model('reply')->getCountByItemID($item['itemid'], 2);
			}
			$result['tweets'] = $beauties;
		}
		echo json_encode($result);die;
	}

	/**
	 * 我的支持
	 */
	public function actionMysupports()
	{
		$request = array();
		$params = array('pageno', 'pagesize', 'userid', 'itemid');
		foreach ($params as $item) {
			$request[$item] = $this->getRequest($item);
		}
		!$request['pageno'] && $request['pageno'] = 1;
		!$request['pagesize'] && $request['pagesize'] = 10;
		$list = $this->model('reply')->getListByReplyerId($request['userid'], 1, $request['pageno'], $request['pagesize'], 'r.userid,u.nickname as truename,u.sign,p.midpath logopath', $request['itemid']);
		if (!empty($list)) {
			foreach ($list as $i => $item) {
				$list[$i]['logopath'] = !empty($list[$i]['logopath']) ? PHOTO_URL . '/' . $item['logopath'] : '';
			}
		}
		$result['code'] = 0;
		$result['msg'] = '成功';
		$result['beauties'] = $list;
		echo json_encode($result);die;
	}
	
	/**
	 * 我的消息
	 */
	public function actionMynews()
	{
		$request = array();
		$params = array('pageno', 'pagesize', 'userid', 'newtype');
		foreach ($params as $item) {
			$request[$item] = $this->getRequest($item);
		}
		!$request['pageno'] && $request['pageno'] = 1;
		!$request['pagesize'] && $request['pagesize'] = 10;
		$newtype = (int) $request['newtype'];
		if (!in_array($newtype, array(1, 2))) {
			$newtype = 1;
		}
		//$list = $this->model('reply')->getListByReplyerId($request['userid'], 2, $request['pageno'], $request['pagesize'], 'r.replyid,r.replyerid,u.truename,p.midpath replyerlogo, r.content,r.replytime createtime');
		$list = $this->model('reply')->getListByUserId1($request['userid'], false, $request['pageno'], $request['pagesize'], $newtype, 'replyid,replyerid,userid,replytype,content');
		if (!empty($list)) {
			foreach ($list as $i => $item) {
				$userid = $item['userid'];
				if ($item['userid'] == $request['userid']) {
					$userid = $item['replyerid'];
					
				} else {
					//$userid = $item['userid'];
					$list[$i]['userid'] = $list[$i]['replyerid'];
				}
				
				$list[$i]['replyerid'] = $userid;
				//unset($list[$i]['replytime']);
				//$list[$i]['createtime'] = date('m-d H:i', $item['replytime']);
				//$list[$i]['content'] = $this->util->sub_string(strip_tags(trim($item['content'])), 0, 30);
				
				$content = $this->model('reply')->getInfoByBoth($item['userid'], $item['replyerid'], 'content,replytime createtime');
				
				$list[$i]['createtime'] = date('m-d H:i', $content['createtime']);
				$list[$i]['content'] = $this->util->sub_string(strip_tags(trim($content['content'])), 0, 30);
				$user = $this->model('user')->getUserPhoto($userid, 'u.nickname as truename,p.midpath replyerlogo, ');
				if (!empty($user)) {
					$list[$i]['truename'] = $user['truename'];
					$list[$i]['replyerlogo'] = !empty($user['replyerlogo']) ? PHOTO_URL . '/' . $user['replyerlogo'] : '';
				} else {
					$list[$i]['truename'] = '';
					$list[$i]['replyerlogo'] = '';
				}
			}
		}
		$result['code'] = 0;
		$result['msg'] = '成功';
		$result['news'] = $list;
		echo json_encode($result);die;
	}
	
	public function actionMyReply()
	{
		$request = array();
		$params = array('pageno', 'pagesize', 'userid');
		foreach ($params as $item) {
			$request[$item] = $this->getRequest($item);
		}
		!$request['pageno'] && $request['pageno'] = 1;
		!$request['pagesize'] && $request['pagesize'] = 10;
		$list = $this->model('reply')->getListByReplyerId($request['userid'], 2, $request['pageno'], $request['pagesize'], 'r.replyid,r.replyerid,u.nickname as truename,p.midpath replyerlogo, r.content,r.replytime createtime');
		if (!empty($list)) {
			foreach ($list as $i => $item) {
				$list[$i]['replyerlogo'] = !empty($list[$i]['replyerlogo']) ? PHOTO_URL . '/' . $item['replyerlogo'] : '';
			}
		}
		$result['code'] = 0;
		$result['msg'] = '成功';
		$result['news'] = $list;
		echo json_encode($result);die;
	}
	
	public function actionNewsnum()
	{
		$userid = (int) $this->getRequest('userid', 0);
		$new_num = $this->model('reply')->getNewsNum($userid);
		$result['code'] = 0;
		$result['msg'] = '成功';
		$result['newsnum'] = $new_num;
		echo json_encode($result);die;
	}
	
	public function actionNewshasread()
	{
		$userid = (int) $this->getRequest('userid', 0);
		if ($this->model('reply')->resetNewsNum($userid) !== false) {
			$this->return_json(0, '成功');
		}
		$this->return_json(1, '失败');
	}
	
	public function actionIsfollow()
	{
		//关注人ID
		$userid = (int) $this->getRequest('userid', 0);
		//被关注人id
		$nsid = (int) $this->getRequest('nsid', 0);
		$result['code'] = 0;
		$result['msg'] = '成功';
		$result['isfollow'] = (int) $this->model('guanzhu')->isExist($userid, $nsid);
		echo json_encode($result);die;
	}
	
	public function actionIspay()
	{
		$request = array();
		$params = array('userid', 'itemid');
		foreach ($params as $item) {
			$request[$item] = $this->getRequest($item);
		}
		$result['code'] = 0;
		$result['msg'] = '成功';
		$result['isfollow'] = '0';
		if ($this->model('Item')->getHowmuch($request['itemid']) > 0) {
			if ($this->model('consume')->isExist($request['userid'], $request['itemid'])) {
				$result['isfollow'] = '1';
			}
		} else {
			$result['isfollow'] = '1';
		}
		
		echo json_encode($result);die;
	}
	
	public function actionMychat()
	{
		$request = array();
		$params = array('pageno', 'pagesize', 'userid', 'replyerid');
		foreach ($params as $item) {
			$request[$item] = $this->getRequest($item);
		}
		!$request['pageno'] && $request['pageno'] = 1;
		!$request['pagesize'] && $request['pagesize'] = 10;
		$list = $this->model('reply')->getListAll($request['userid'], $request['replyerid'], $request['pageno'], $request['pagesize'], 'r.userid,r.replyerid,r.itemid,u.nickname as truename,u.sign,p.midpath replyerlogo,r.replytime createtime,r.content');
		if (!empty($list)) {
			$user = $this->model('User')->getUserPhoto($request['userid'], 'p.path');
			$replyer = $this->model('User')->getUserPhoto($request['replyerid'], 'p.path');
			foreach ($list as $i => $item) {
				$list[$i]['replyerlogo'] = !empty($list[$i]['replyerlogo']) ? PHOTO_URL . '/' . $item['replyerlogo'] : '';
				$list[$i]['createtime'] = date('m-d H:i', $item['createtime']);
				$photo = $this->model('User')->getUserPhoto($item['userid'], 'p.midpath userlogo');
				$list[$i]['userlogo'] = !empty($photo['userlogo']) ? PHOTO_URL . '/' . $photo['userlogo'] : '';;
			}
		}
		$result['code'] = 0;
		$result['msg'] = '成功';
		$result['news'] = $list;
		echo json_encode($result);die;
	}
}